// __multiversion__
// This signals the loading code to prepend either #version 100 or #version 300 es as apropriate.

// To use centroid sampling we need to have version 300 es shaders, which requires changing:
// attribute to in
// varying to out when in vertex shaders or in when in fragment shaders
// defining an out vec4 FragColor and replacing uses of gl_FragColor with FragColor
// texture2D to texture
#if __VERSION__ >= 300
	#define attribute in
	#define varying out

	#ifdef MSAA_FRAMEBUFFER_ENABLED
		#define _centroid centroid
	#else
		#define _centroid
	#endif

	#ifndef BYPASS_PIXEL_SHADER
		_centroid out vec2 uv0;
		_centroid out vec2 uv1;
	#endif
#else
	#ifndef BYPASS_PIXEL_SHADER
		varying vec2 uv0;
		varying vec2 uv1;
	#endif
#endif

#ifndef BYPASS_PIXEL_SHADER
	varying vec4 color;
#endif

#ifdef FOG 
	varying vec4 fogColor;
#endif

#ifdef NEAR_WATER
	varying float cameraDist;
#endif

varying float flag_waterplane;
varying float new_fog;

varying vec3 screen;

varying highp vec3 position;
varying highp vec3 look;

varying highp vec3 w_pos;




#ifdef AS_ENTITY_RENDERER
	uniform MAT4 WORLDVIEWPROJ;
#else
	uniform MAT4 WORLDVIEW;
	uniform MAT4 PROJ;
#endif
uniform vec4 FOG_COLOR;
uniform vec2 FOG_CONTROL;
uniform float RENDER_DISTANCE;
uniform vec2 VIEWPORT_DIMENSION;
uniform vec4 CURRENT_COLOR;
uniform POS4 CHUNK_ORIGIN_AND_SCALE;
uniform POS3 VIEW_POS;
uniform float FAR_CHUNKS_DISTANCE;
uniform highp float TIME;
uniform int FOG_MODE;

attribute POS4 POSITION;
attribute vec4 COLOR;
attribute vec2 TEXCOORD_0;
attribute vec2 TEXCOORD_1;

bool getNetherMask(vec4 ambient){
if(ambient.r > ambient.b && ambient.r < 0.5 && ambient.b < 0.05){
return true;
} else {
return false;}
}

bool getTheEndMask(vec4 ambient){
if(ambient.r > ambient.g && ambient.b > ambient.g && ambient.r < 0.05 && ambient.b < 0.05 && ambient.g < 0.05){
return true;
} else {
return false;}
}

bool getUnWaterMask(vec4 ambient){
if(ambient.b > ambient.r*2.5 &&  ambient.b*3.0 > ambient.g){
return true;
} else {
return false;}
}


void main()
{



#ifdef BYPASS_PIXEL_SHADER
	gl_Position = vec4(0, 0, 0, 0);
	return;
#else


    POS4 worldPos;
#ifdef AS_ENTITY_RENDERER
		POS4 pos = WORLDVIEWPROJ * POSITION;
		worldPos = pos;
#else
    worldPos.xyz = (POSITION.xyz * CHUNK_ORIGIN_AND_SCALE.w) + CHUNK_ORIGIN_AND_SCALE.xyz;
    worldPos.w = 1.0;

position = worldPos.xyz + VIEW_POS.xyz;
look = worldPos.xyz;

float fractposy = fract(position.y);
if(fractposy < 0.1 && fractposy >= 0.0){
flag_waterplane = 1.0;
}
else{
flag_waterplane = 0.0;
}

POS4 pos = WORLDVIEW * worldPos;
pos = PROJ * pos;

#endif
screen.st = pos.st / (pos.z + 0.8);
    gl_Position = pos;
    

float debugfog = 0.0;
POS3 pp = worldPos.xyz + VIEW_POS.xyz;

    uv0 = TEXCOORD_0;
    uv1 = TEXCOORD_1;
	color =  COLOR;


float wav1 = sin(TIME * 4.0 + pp.x+pp.z+pp.x+pp.z+pp.y) * sin(pp.z) * 0.015;

float wav2 = 0.06*sin(6.0*(TIME*0.2+pp.x/0.25+pp.z/1.0))+0.09*sin(6.0*(TIME*0.3+pp.x/5.0-pp.z/2.0))+0.09*sin(6.0*(TIME*0.2-pp.x/7.0+pp.z/4.0));

#ifdef ALPHA_TEST
if(color.g + color.g > color.r + color.b){
gl_Position += wav1;
}
#endif
vec3 wave = worldPos.xyz + VIEW_POS;
#ifdef ALPHA_TEST
if(color.g > color.b)
{
gl_Position.y += sin(color.r + TIME * 2. + wave.z * wave.x) * .08;
}
#endif

#ifdef BLEND
if(color.b > color.r && position.y < 0.5)
{

gl_Position.y += cos(TIME * 0.5 - wave.x + wave.y) * 0.15;
gl_Position.x += cos(TIME * 0.5 + wave.x * wave.y + wave.z) * 0.08;

}
#endif

if((FOG_CONTROL.x < 0.02))
{

gl_Position.x += cos(TIME * 0.2 + wave.y * wave.z * wave.x) * 0.1;

}

if((FOG_CONTROL.x < 0.02))
{

gl_Position.y += cos(TIME * 0.2 + wave.y * wave.z * wave.x) * 0.1;

}


#if defined(FOG) || defined(NEAR_WATER)
	#ifdef FANCY
		vec3 relPos = -worldPos.xyz;
		float cameraDepth = length(relPos);
		#ifdef NEAR_WATER
			cameraDist = cameraDepth / FAR_CHUNKS_DISTANCE;
		#endif
	#else
		float cameraDepth = pos.z;
		#ifdef NEAR_WATER
			vec3 relPos = -worldPos.xyz;
			float camDist = length(relPos);
			cameraDist = camDist / FAR_CHUNKS_DISTANCE;
		#endif
	#endif
#endif




#ifdef FOG
float len = cameraDepth / RENDER_DISTANCE;

#ifdef ALLOW_FADE
len += CURRENT_COLOR.r;
#endif

fogColor.rgb = FOG_COLOR.rgb;
fogColor.a = clamp((len - FOG_CONTROL.x) / (FOG_CONTROL.y - FOG_CONTROL.x), 0.0, 1.0);

if(getNetherMask(FOG_COLOR)){
fogColor.a = clamp((len - FOG_CONTROL.x*0.5), 0.0,1.0);
fogColor.rgb += vec3(0.2)*1.5;
debugfog = 1.0;
}

if(getUnWaterMask(FOG_COLOR)){
if(debugfog==1.0){
fogColor.a = clamp((len - FOG_CONTROL.x), 0.0,0.6);
fogColor.rgb /= FOG_COLOR.rgb;
fogColor.rgb *= vec3(0.8, 0.8, 1.0);
}
if(uv1.y<0.8749){
color.rgb+=(vec3(wav2)*2.0)*uv1.y;
gl_Position.y += wav1;
}}
#endif

vec3 rel = -worldPos.xyz*0.5;
float addfog = length(rel);
float RD = 0.2;
if(getTheEndMask(FOG_COLOR)){RD = 0.5, 0.5, 0.5;}
new_fog = addfog / FAR_CHUNKS_DISTANCE*RD;

#endif
}